/**
 * 
 */
package kr.co.insoft.auth.model.members.validator;

import kr.co.insoft.auth.model.members.JoinFormModel;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Component
public class JoinFormValidator implements Validator {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@Override
	public boolean supports(Class<?> clazz) {
		return JoinFormModel.class.equals(clazz);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.validation.Validator#validate(java.lang.Object,
	 * org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object target, Errors errors) {
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username",
				"validate.not.empty.member.username");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "nickname",
				"validate.not.empty.member.nickname");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
				"validate.not.empty.member.password");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword",
				"validate.not.empty.member.password_confirm");
		JoinFormModel joinFormModel = (JoinFormModel) target;
		if (StringUtils.hasLength(joinFormModel.getPassword()))
			if (!joinFormModel.getPassword().equals(
					joinFormModel.getConfirmPassword())) {
				errors.rejectValue("confirmPassword",
						"validate.not.equlas.password");
			}
		// 사용자 중복체크해야함
	}

}
